home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*
- | |
- | <<< PHeap.h - Heap Manager Routines Header File >>> |
- | |
- *---------------------------------------------------------------------------*/
-
-
- #ifndef __PHEAP__
- #define __PHEAP__
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- /*
- NOTE: PLEASE READ THE FOLLOWING BEFORE USING THIS HEADER FILE:
-
- It is assumed that by including Heap.h in your program, your memory management
- consists only of allocations, and deallocations of all allocations beyond a
- specified point. In other words your memory management assumes a Pascal-like
- heap management that allows new, mark, and release. That is precisely what
- the Heap Manager provides!
-
- The Heap Manager is an efficient memory allocation scheme that only assumes
- allocations with new, remembering a location with mark, and releasing memory
- back to the "mark" with release. It can be more efficient than the more
- general malloc and free because of the simple memory model imposed by this
- form of allocation. A malloc/free model (or in Pascal new/dispose) requires
- more overhead per allocation and more code to maintain allocated and freed
- blocks. Note, however, use of the Heap Manager does NOT preclude use of
- malloc and free. Both memory models may coexist simultaneously without
- conflict (other than possible fragmentation due to differences in the sizes
- of the underlying memory allocations utilized by each of the model's
- algorithms). */
-
- /* This file contains:
-
- initHeap(size, delta, nonCont, proc) - Heap Manager inititialization
- New(amount) - Allocate amount space on the heap
- Mark(p) - Set p to point at current heap position
- Release(p) - Free heap space back to p
-
- */
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
-
- extern OSErr initHeap(long initSize, long delta, Boolean nonCont,
- void (*errProc)());
- /*
- Call this proc to initialize the Heap Manager. The parameters detemine
- certain characteristics of the way the heap is allocated.
-
- The initSize parameter determines the initial size (in bytes) of the
- unallocated heap. When this space if fully allocated an attempt is made to
- extend this space contiguously with the original space. If that extension is
- unsuccessful, and the parameter nonCont is set to true, a new "chunk" of
- (generally, non-contiguous) heap space is allocated. The size of this
- additional heap space is specified by the delta parameter (again the size is
- in bytes).
-
- If anything goes wrong during allocation of the heap or space within it, for
- example, additional contiguous space cannot be allocated and nonCont was set
- false, then the Heap Manager uses the errProc to report the error to the
- caller. The errProc call can be done from initHeap, new, or release, and they
- assume the following declaration:
-
- void errProc(heapResult)
- OSErr heapResult;
- {
- - - -
- }
-
- The heapResult is an error number appropriate to the situation usually
- reported by the underlying system memory management routines (the routines the
- Heap Manager itself uses). The errProc may or may not return to the Heap
- Manager. If it does, error reporting from the individual Heap Manager
- routines is the same as if no errProc was supplied which is the case we will
- describe shortly. Either way, the heapResult code of the most recent Heap
- Manager call is available in the global heapResult: */
-
- extern OSErr heapResult; /* Most recent heap error result code */
- /*
- If there is no errProc, i.e., NULL is specified, or, as we just said, the
- errProc returns, then error reporting is a function of each routine and is
- discussed with the description of that routine. In the case of initHeap,
- an error code (the same one passed to the errProc) is returned as the
- function result, or 0 if there is no error.
-
- The only error that could result from calling initHeap is a failure to
- allocate the initial heap of initSize bytes. It is assumed initHeap is called
- only once, and at the beginning of execution. Thus, generally you don't have
- to check the return code because, if the initial allocation fails, something
- is seriously wrong with your system, and something else is also bound to fail
- that you will check!
-
- If initHeap is NOT called, then it will be automatically called on the first
- call to new. The call will be done as,
-
- initHeap(initalHeap, heapDelta, nonContiguous, memErrProc);
-
- (and, by the way, the return code IS checked by new -- new has its own way of
- reporting errors to its caller). The parameters are global variables that
- YOU may set, i.e., */
-
- extern long initalHeap; /* Size of initial heap chunk */
- extern long heapDelta; /* Size of additional heap chunks */
- extern Boolean nonContiguous; /* true ==> allow non-contiguous chunks */
- extern void (*memErrProc)(); /* Ptr to memory error proc */
- /*
- Thus you may bypass the explicit initHeap, letting new do it, with values
- that you can set. If you don't set them then they are defaulted to give
- an initHeap call of initHeap(10000, 10000, true, NULL);
- */
-
-
- extern char *New(long amount);
- /*
- Allocate amount bytes of heap space. The amount is rounded up to an even
- value if necessary. If the space cannot be allocated NULL is returned (over
- and above any error reporting done by the errProc specified to initHeap).
-
- The initHeap routine is called on the first call to new if initHeap was not
- explicitly called prior to calling new.
- */
-
-
- extern void *Mark(void **p);
- /*
- Set a pointer to the heap area. The current "top" of heap is returned in the
- parameter p and as the function result. A subsequent call to release will
- free all heap space from this point, p, on.
- */
-
-
- extern void Release(void *p);
- /*
- Deallocate all heap space back to the specified "mark", p. It is assumed p
- is a value set by a mark call. All space from allocated after that mark call
- is deallocated. Pointers to that area should NOT be used again! Errors are
- not explicitly reported from a call to this proc except perhaps through the
- errProc specified to initHeap.
- */
-
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif